import json

from django.http import HttpResponse

# Create your views here.
from chat_bot.ChatBot import ChatBot
from chat_bot.answer_search.graph_querier import get_graph


def index(request):
    bot = ChatBot()
    if request.method == "POST":
        text = None

        if text is None:
            text = request.GET.get("text")

        if text is None:
            text = request.POST.get("text")

        if text is None:
            try:
                json_str = request.body.decode('UTF-8')
                json_dict = json.loads(json_str)
                text = json_dict.get("text")
            except Exception:
                text = ''

        return HttpResponse(str(bot(text)))
    return HttpResponse(json.dumps({'code': 1, "meta": 200, "msg": "something wrong!"}))


def entity(request):
    json_str = request.body.decode('UTF-8')
    json_dict = json.loads(json_str)
    return HttpResponse(json.dumps(get_graph(json_dict.get("entity"))), content_type="application/json")
